package com.jumi.microservice.stock;

import com.jumi.microservice.domain.GoodsStockDO;
import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.mapper.GoodsStockMapper;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 商品库存更新命令的抽象基类
 *
 * @author Administrator
 */
public abstract class AbstractSupplyStockUpdater implements StockUpdater {

    private static final Logger logger = LoggerFactory.getLogger(AbstractSupplyStockUpdater.class);

    /**
     * 商品库存DO对象
     */
    protected List<GoodsStockDO> goodsStocks;

    /**
     * 商品sku分仓库存DO对象
     */
    protected List<GoodsWarehouseStockDO> goodsWarehouseStocks;

    /**
     * 商品库存管理模块的mapper组件
     */
    protected GoodsStockMapper goodsStockMapper;

    /**
     * 商品分仓库存管理模块的mapper组件
     */
    protected GoodsWarehouseStockMapper goodsWarehouseStockMapper;

    /**
     * 构造函数
     *
     * @param goodsStocks               商品库存DO对象
     * @param goodsWarehouseStocks      商品sku分仓库存DO对象
     * @param goodsStockMapper          商品库存管理模块的mapper组件
     * @param goodsWarehouseStockMapper 商品分仓库存管理模块的mapper组件
     */
    public AbstractSupplyStockUpdater(List<GoodsStockDO> goodsStocks,
                                      List<GoodsWarehouseStockDO> goodsWarehouseStocks,
                                      GoodsStockMapper goodsStockMapper,
                                      GoodsWarehouseStockMapper goodsWarehouseStockMapper) {
        this.goodsStocks = goodsStocks;
        this.goodsWarehouseStocks = goodsWarehouseStocks;
        this.goodsStockMapper = goodsStockMapper;
        this.goodsWarehouseStockMapper = goodsWarehouseStockMapper;
    }

    /**
     * 更新商品库存
     */
    @Override
    public Boolean updateGoodsStock() {
        try {
            updateSaleStockQuantity();
            updatePreStockQuantity();
            updateWarehouseStockQuantity();
            executeUpdateGoodsStock();
            executeUpdateWarehouseGoodsStock();
        } catch (Exception e) {
            logger.error("error", e);
        }
        return true;
    }

    /**
     * 更新商品的销售库存
     *
     * @throws Exception
     */
    protected abstract void updateSaleStockQuantity() throws Exception;

    /**
     * 更新商品的预售库存
     *
     * @throws Exception
     */
    protected abstract void updatePreStockQuantity() throws Exception;

    /**
     * 更新商品的分仓库存
     *
     * @throws Exception
     */
    protected abstract void updateWarehouseStockQuantity() throws Exception;
    /**
     * 实际执行更新商品库存的操作
     *
     * @throws Exception
     */
    private void executeUpdateGoodsStock() throws Exception {
        for (GoodsStockDO goodsStock : goodsStocks) {
            goodsStock.setUpdateTime(LocalDateTime.now());
            goodsStockMapper.updateById(goodsStock);
        }
    }

    /**
     * 实际执行更新商品分仓库存的操作
     *
     * @throws Exception
     */
    private void executeUpdateWarehouseGoodsStock() throws Exception {
        for (GoodsWarehouseStockDO goodsWarehouseStock : goodsWarehouseStocks) {
            goodsWarehouseStock.setUpdateTime(LocalDateTime.now());
            goodsWarehouseStockMapper.updateById(goodsWarehouseStock);
        }
    }
}
